/**
 * Copyright (c) 2007-2008 Nearchos Paspallis
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * AspectSense
 *
 * Middleware for annotation-based, context-aware and adaptive services for
 * mobile and ubiquitous computing
 *
 * This project is an open-source effort, initiated by Nearchos Paspallis as
 * part of his PhD work at the University of Cyprus (http://www.ucy.ac.cy).
 *
 * More information about the project is available at "http://aspectsense.org".
 */

package org.aspectsense.example_apps.digitalassistant;

import javax.swing.*;
import java.io.Serializable;
import java.util.Date;
import java.util.Arrays;
import java.text.DateFormat;
import java.awt.*;

/**
 * @author Nearchos Paspallis [nearchos@cs.ucy.ac.cy]
 *         Date: Feb 26, 2008
 *         Time: 12:17:21 PM
 */
public class Appointment implements Serializable, Comparable
{
    private final String name;
    private final Date fromDate;
    private final Date toDate;
    private final String where;
    private final String [] participants;

    public Appointment(
            final String name,
            final Date fromDate,
            final Date toDate,
            final String where,
            final String [] participants)
    {
        this.name = name;
        this.fromDate = fromDate;
        this.toDate = toDate;
        this.where = where;
        this.participants = participants;
    }

    public String getName()
    {
        return name;
    }

    public Date getFromDate()
    {
        return fromDate;
    }

    public Date getToDate()
    {
        return toDate;
    }

    public String getWhere()
    {
        return where;
    }

    public String [] getParticipants()
    {
        return participants;
    }

    public int compareTo(final Object otherObject)
    {
        if(otherObject == null)
        {
            throw new NullPointerException("Illegal null argument");
        }

        if(otherObject == this) return 0;

        if(otherObject.getClass() != this.getClass())
        {
            throw new IllegalArgumentException("Comparable objects must be of type: " + Appointment.class);
        }

        final Appointment otherAppointment = (Appointment) otherObject;

        return fromDate.compareTo(otherAppointment.fromDate);
    }


    public boolean equals(Object o)
    {
        if (this == o)
        {
            return true;
        }
        if (!(o instanceof Appointment))
        {
            return false;
        }

        Appointment that = (Appointment) o;

        if (fromDate != null ? !fromDate.equals(that.fromDate) : that.fromDate != null)
        {
            return false;
        }
        if (name != null ? !name.equals(that.name) : that.name != null)
        {
            return false;
        }
        if (!Arrays.equals(participants, that.participants))
        {
            return false;
        }
        if (toDate != null ? !toDate.equals(that.toDate) : that.toDate != null)
        {
            return false;
        }
        if (where != null ? !where.equals(that.where) : that.where != null)
        {
            return false;
        }

        return true;
    }

    public int hashCode()
    {
        int result;
        result = (name != null ? name.hashCode() : 0);
        result = 31 * result + (fromDate != null ? fromDate.hashCode() : 0);
        result = 31 * result + (toDate != null ? toDate.hashCode() : 0);
        result = 31 * result + (where != null ? where.hashCode() : 0);
        result = 31 * result + (participants != null ? Arrays.hashCode(participants) : 0);
        return result;
    }

    private final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
    private final DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT);

    private String getWhenAsText()
    {
        final String fromDateS = dateFormat.format(fromDate);
        final String toDateS = dateFormat.format(toDate);
        final String fromTimeS = timeFormat.format(fromDate);
        final String toTimeS = timeFormat.format(toDate);
        String when;
        if(fromDateS.equals(toDateS))
        {
            return "on " + fromDateS + ", from " + fromTimeS + " to " + toTimeS;
        }
        else
        {
            return "from " + fromDateS + ", " + fromTimeS + " to " + toDateS + ", " + toTimeS;
        }
    }

    public String toHTML()
    {
        final StringBuilder htmlBuilder = new StringBuilder();
        htmlBuilder.append("<html><body><h2>").append(name).append("</h2>");
        htmlBuilder.append("<i>Where</i>: ").append(where).append("<br/>");
        htmlBuilder.append("<i>When</i>: ").append(getWhenAsText()).append("<br/>");
        htmlBuilder.append("<i>Participants</i>: <ol>");
        for(final String participant : participants)
        {
            htmlBuilder.append("<li>").append(participant).append("</li>");
        }
        htmlBuilder.append("</ol></body></html>");

        return htmlBuilder.toString();
    }

    public String toSpeakableText()
    {
        final int numOfParticipants = participants.length;

        final StringBuilder textBuilder = new StringBuilder();
        textBuilder.append("Appointment titled ").append(name);
        textBuilder.append(" scheduled ").append(getWhenAsText());
        textBuilder.append(" and taking place at ").append(where);
        textBuilder.append(". There ").append(numOfParticipants == 1 ? "is " : "are ");
        textBuilder.append(numOfParticipants).append(" participant");
        textBuilder.append(numOfParticipants == 1 ? "" : "s");
        textBuilder.append(" to this appointment: ");
        for(int i = 0; i < numOfParticipants; i++)
        {
            textBuilder.append(participants[i]);
            if(numOfParticipants > 1)
            {
                if(i < (numOfParticipants - 2))
                {
                    textBuilder.append(", ");
                }
                else if(i == (numOfParticipants - 1))
                {
                    // do nothing
                }
                else
                {
                    textBuilder.append(" and ");
                }
            }
        }

        return textBuilder.toString();
    }
}